#!/bin/sh
####################################################################
#                                                                  #
# THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE.   #
# USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     #
# GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE #
# IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       #
#                                                                  #
# THE Theora SOURCE CODE IS COPYRIGHT (C) 2009,2025                #
# by the Xiph.Org Foundation and contributors                      #
# https://www.xiph.org/                                            #
#                                                                  #
####################################################################

ffmpeg=ffmpeg
if ! command -v $ffmpeg 2>&1 >/dev/null ; then
        echo error: $ffmpeg executable not found!
        exit 1
fi
ffprobe=ffprobe
if ! command -v $ffprobe 2>&1 >/dev/null ; then
        echo error: $ffprobe executable not found!
        exit 1
fi


# TODO: get script dir, and call encoder_example from the same dir, to
#       support both system installed as well as local dir version

# check encoder_example as well as Debian named theora_encoder_example
if command -v encoder_example 2>&1 >/dev/null ; then
        encoder_example=encoder_example
elif command -v theora_encoder_example 2>&1 >/dev/null ; then
        encoder_example=theora_encoder_example
else
        echo error: encoder_example or theora_encoder_example executable \
not found!
        exit 1
fi

if [ -z "$2" ] ; then
        echo usage:
        echo $0 inputfile outputfile [$encoder_example encoder options]
        echo
        echo for $encoder_example encoder options run:
        echo $encoder_example -h
        exit
fi

inputfile=$1
outputfile=$2
shift 2

INPUT_AUDIO=0
# check if there is audio in the input file, then encoder_example
# needs a different syntax
$ffprobe -i $inputfile -show_streams -select_streams a -loglevel error | \
    grep -q audio && INPUT_AUDIO=1

video=$(mktemp -u)
mkfifo -m 600 $video

# TODO: merge the two separate ffmpeg commands to a single process
$ffmpeg -i $inputfile -y -hide_banner -loglevel error -an \
	-f yuv4mpegpipe $video &

if [ "$INPUT_AUDIO" = 1 ] ; then
        audio=$(mktemp -u)
        mkfifo -m 600 $audio

        $ffmpeg -i $inputfile -y -hide_banner -loglevel error -vn -f wav \
		-bitexact $audio &

        $encoder_example $@ $audio $video -o $outputfile

        rm -f $audio
else
        $encoder_example $@ $video -o $outputfile
fi

rm -f $video
